package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = {"购物车相关接口"})
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("保存购物车数据")
    public Result save(@RequestBody ShoppingCartDTO dto){
        log.info("添加购物车{}",dto);
        shoppingCartService.addShoppingCart(dto);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("展示购物车数据")
    public Result<List<ShoppingCart>> queryAll(){
        Long currentId = BaseContext.getCurrentId();
        log.info("当前用户{}查询购物车",currentId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.queryAll();
        return Result.success(shoppingCarts);
    }

    @PostMapping("/sub")
    @ApiOperation("删除购物车单项数据")
    public Result deleteItem(@RequestBody ShoppingCartDTO dto){
        log.info("删除{}购物车",dto);
        shoppingCartService.deleteItem(dto);
        return Result.success();
    }
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result deleteAll(){
        log.info("清空购物车");
        shoppingCartService.deleteAll();
        return Result.success();
    }
}
